package ruifight.member.controller;

import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import ruifight.member.model.ClassifyInfo;
import ruifight.member.model.ClassifyInfoExample;
import ruifight.member.service.AccountRecordService;
import ruifight.member.service.ClassifyInfoService;
import ruifight.member.util.ObjUtil;

import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping("/interface")
public class ClassifyInfoController {

	private static final Logger logger = LoggerFactory.getLogger(ClassifyInfoController.class);

	@Autowired
	public ClassifyInfoService classifyInfoService;

	/**
	 * 获取分类列表
	 * @return
	 */
	@RequestMapping("/getClassifyInfoList")
	@ResponseBody
	public Map<String, Object> getClassifyInfoList(@RequestBody Map<String, Object> paramMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		String cur_page= paramMap.get("cur_page").toString();
		String page_size= paramMap.get("page_size").toString();
		PageInfo<Map<String, Object>> pageInfo = classifyInfoService.getClassifyInfoList(Integer.parseInt(cur_page), Integer.parseInt(page_size), paramMap);
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return map;
	}

	/**
	 * 添加分类/标签
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/addClassifyInfo")
	@ResponseBody
	public Map<String, Object> addClassifyInfo(@RequestBody Map<String, String> paramMap){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ClassifyInfo classifyInfo = new ClassifyInfo();
			classifyInfo.setClassifyName(paramMap.get("classifyName"));
			classifyInfo.setClassifyType(Integer.parseInt(paramMap.get("classifyType")));
			if(ObjUtil.isNotNullAndEmpty(paramMap.get("parentClassifyInfoId"))){
				classifyInfo.setParentClassifyInfoId(Long.parseLong(paramMap.get("parentClassifyInfoId")));
				classifyInfo.setParentClassifyName(paramMap.get("parentClassifyName"));
			}
			classifyInfo.setAttr2(paramMap.get("attr2"));
			classifyInfo.setClassifyState(1);
			classifyInfo.setCreateDate(new Date());
			classifyInfoService.insertSelective(classifyInfo);
			map.put("state",1);
			map.put("msg","添加成功");
		}catch (Exception e){
			map.put("state",0);
			map.put("msg","添加失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 修改分类名称
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/updateClassifyInfo")
	@ResponseBody
	public Map<String, Object> updateClassifyInfo(@RequestBody Map<String, String> paramMap){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ClassifyInfo classifyInfo = new ClassifyInfo();
			classifyInfo.setClassifyInfoId(Long.parseLong(paramMap.get("classifyInfoId")));
			classifyInfo.setClassifyName(paramMap.get("classifyName"));
			classifyInfo.setAttr2(paramMap.get("attr2"));
			classifyInfoService.updateByPrimaryKeySelective(classifyInfo);
			map.put("state",1);
			map.put("msg","修改成功");
		}catch (Exception e){
			map.put("state",0);
			map.put("msg","修改失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 删除分类/标签
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/delClassifyInfo")
	@ResponseBody
	public Map<String, Object> delClassifyInfo(@RequestBody Map<String, String> paramMap){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ClassifyInfo classifyInfo = new ClassifyInfo();
			classifyInfo.setClassifyInfoId(Long.parseLong(paramMap.get("classifyInfoId")));
			classifyInfo.setClassifyState(0);
			classifyInfoService.updateByPrimaryKeySelective(classifyInfo);
			map.put("state",1);
			map.put("msg","删除成功");
		}catch (Exception e){
			map.put("state",0);
			map.put("msg","删除失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 获取标签
	 * @return
	 */
	@RequestMapping("/getTagInfoList")
	@ResponseBody
	public List<ClassifyInfo> getTagInfoList(@RequestBody Map<String, Object> paramMap) {
		List<ClassifyInfo> list = classifyInfoService.getClassifyAndTagInfoList(paramMap);
		return list;
	}
	/**
	 * 获取分类列表
	 * @return
	 */
	@RequestMapping("/getClassifyList")
	@ResponseBody
	public List<ClassifyInfo> getClassifyList(@RequestBody Map<String, Object> paramMap) {
		List<ClassifyInfo> list = classifyInfoService.getClassifyAndTagInfoList(paramMap);
		return list;
	}

	/**
	 * 上移，下移
	 * @return
	 */
	@RequestMapping("/move")
	@ResponseBody
	public Map<String,Object> move(@RequestBody Map<String, Object> paramMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String classifyInfoId = paramMap.get("classifyInfoId").toString();
//		String createDate = paramMap.get("createDate").toString();
		String moveType = paramMap.get("moveType").toString();
		ClassifyInfo classify = classifyInfoService.selectByPrimaryKey(Long.parseLong(classifyInfoId));
		try{
			//上移
			if(moveType.equals("1")){
				//查出大于当前时间的第一条
				ClassifyInfoExample classifyInfoExample = new ClassifyInfoExample();
				ClassifyInfoExample.Criteria criteria = classifyInfoExample.createCriteria();
				criteria.andClassifyTypeEqualTo(1);
				criteria.andCreateDateGreaterThan(classify.getCreateDate());
				ClassifyInfo classifyInfo = classifyInfoService.selectOneByExample(classifyInfoExample);
				//被动createDate
				Date date = classifyInfo.getCreateDate();
				classifyInfo.setCreateDate(classify.getCreateDate());
				ClassifyInfo classifyInfo2 = new ClassifyInfo();
				classifyInfo2.setClassifyInfoId(Long.parseLong(classifyInfoId));
				classifyInfo2.setCreateDate(date);
				classifyInfoService.updateByPrimaryKeySelective(classifyInfo);
				classifyInfoService.updateByPrimaryKeySelective(classifyInfo2);
			}else{
				//查出小于当前时间的第一条
				ClassifyInfoExample classifyInfoExample = new ClassifyInfoExample();
				classifyInfoExample.setOrderByClause("createDate DESC");
				ClassifyInfoExample.Criteria criteria = classifyInfoExample.createCriteria();
				criteria.andClassifyTypeEqualTo(1);
				criteria.andCreateDateLessThan(classify.getCreateDate());
				ClassifyInfo classifyInfo = classifyInfoService.selectOneByExample(classifyInfoExample);
				//被动createDate
				Date date = classifyInfo.getCreateDate();
				classifyInfo.setCreateDate(classify.getCreateDate());
				ClassifyInfo classifyInfo2 = new ClassifyInfo();
				classifyInfo2.setClassifyInfoId(Long.parseLong(classifyInfoId));
				classifyInfo2.setCreateDate(date);
				classifyInfoService.updateByPrimaryKeySelective(classifyInfo);
				classifyInfoService.updateByPrimaryKeySelective(classifyInfo2);
			}
		}catch (Exception e){
			e.printStackTrace();
		}

		return map;
	}

	/**
	 * 移动
	 * @return
	 */
	@RequestMapping("/move2")
	@ResponseBody
	public Map<String,Object> move2(@RequestBody Map<String, Object> paramMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		try{
			Long newIndex = Long.parseLong(paramMap.get("newIndex").toString());
			Date newDate = sdf.parse(paramMap.get("newDate").toString());
			Long oldIndex = Long.parseLong(paramMap.get("oldIndex").toString());
			Date oldDate = sdf.parse(paramMap.get("oldDate").toString());

			//下移
			if(newIndex > oldIndex){
				ClassifyInfoExample classifyInfoExample = new ClassifyInfoExample();
				classifyInfoExample.setOrderByClause("createDate DESC");
				ClassifyInfoExample.Criteria criteria = classifyInfoExample.createCriteria();
				criteria.andClassifyTypeEqualTo(1);
				criteria.andCreateDateGreaterThan(newDate);
				List<ClassifyInfo> classifyInfoList = classifyInfoService.selectByExample(classifyInfoExample);
				Date temp; // 记录临时中间值
				int size = classifyInfoList.size(); // 数组大小
				for(int i = 1; i < size; i++) {
					for(int j = 0; j < size-i; j++) {
						if(classifyInfoList.get(i).getCreateDate().compareTo(classifyInfoList.get(j+1).getCreateDate()) != 1 ) {
							temp = classifyInfoList.get(j).getCreateDate();
							classifyInfoList.get(j).setCreateDate(classifyInfoList.get(j+1).getCreateDate());
							classifyInfoList.get(j+1).setCreateDate(temp);
						}
					}
				}
			}else{

			}
		}catch (Exception e){
			e.printStackTrace();
		}

		return map;
	}

}
